

module.exports = {
  getSearchlist: getSearchlist,
  getDateArry:getDateArry
};


function getSearchlist(data, pageNo, pageSize) {
  var len = pageSize < data.length - (pageNo - 1) * pageSize ? pageSize : data.length - (pageNo - 1) * pageSize;
  var list = new Array(len);
  for(var i = 0; i < len; i++){
    list[i] = data[(pageNo - 1) * pageSize + i];
  }
  return list;
}

function getDateArry(len, type){
  var today = new Date();
  var year = today.getFullYear();
  var month = (today.getMonth() + 1);
  var day = (today.getDate());

  if(type == 'year') {
    var result = [];
    for(var i = len; i--; ) {
      result.push({y: year - i + ''});
    }
    return result;
  } else if(type == 'month') {
    var result = [];
    for(var i = len; i--; ) {
      if(month - i > 0) {
        result.push({y: year + '-' + (month - i)});
      } else {
        result.push({y: (year - 1) + '-' + (12 + month - i)});
      }
    }
    return result;
  } else if(type == 'day'){
    var result = [];
    for(var i = len; i--; ) {
      if(day - i > 0) {
        result.push({y: year + '-' + month + '-' + (day - i)});
      } else {
        result.push({y: year + '-' + month + '-' + (getMonthDay(year, month) + day - i)});
      }
    }
    return result;
  }
}


function getMonthDay(year, month) {
  if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    return 31;
  } else if(month == 4 || month == 6 ||month == 9 ||month == 11) {
    return 30;
  } else if(month == 2 && year % 4 == 0) {
    return 29;
  } else if(month == 2) {
    return 28;
  }
}